package de.tuberlin.project.oose.figures;

import org.jhotdraw.draw.*;
import org.jhotdraw.draw.text.FloatingTextField;
import org.jhotdraw.draw.tool.AbstractTool;

import java.awt.*;
import java.awt.event.*;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.UndoableEdit;

import org.jhotdraw.util.ResourceBundleUtil;

import de.tuberlin.project.oose.figures.fields.OurFloatingTextField;

public class VornameTextEditingTool extends AbstractTool implements
		ActionListener {

	private OurFloatingTextField textField;
	private TextHolderFigure typingTarget;

	/** Creates a new instance. */
	public VornameTextEditingTool(TextHolderFigure typingTarget) {
		this.typingTarget = typingTarget;
	}

	@Override
	public void deactivate(DrawingEditor editor) {
		endEdit();
		super.deactivate(editor);
	}

	/**
	 * If the pressed figure is a TextHolderFigure it can be edited.
	 */
	@Override
	public void mousePressed(MouseEvent e) {
		if (typingTarget != null) {
			beginEdit(typingTarget);
			updateCursor(getView(), e.getPoint());
		}
	}

	protected void beginEdit(TextHolderFigure textHolder) {
		if (textField == null) {
			textField = new OurFloatingTextField();
			textField.addActionListener(this);
			System.out.println(textField.getPreferredSize(1));
		}

		if (textHolder != typingTarget && typingTarget != null) {
			endEdit();
		}

		textField.createOverlay(getView(), textHolder);
		textField.requestFocus();
		typingTarget = textHolder;
	}

	@Override
	public void mouseReleased(MouseEvent evt) {
	}

	protected void endEdit() {
		if (typingTarget != null) {
			typingTarget.willChange();

			final TextHolderFigure editedFigure = typingTarget;
			final String oldText = typingTarget.getText();
			final String newText = textField.getText();

			if (newText.length() >= 0) {
				typingTarget.willChange();
				typingTarget.setText(newText);
				typingTarget.changed();
			}
			UndoableEdit edit = new AbstractUndoableEdit() {

				@Override
				public String getPresentationName() {
					ResourceBundleUtil labels = ResourceBundleUtil
							.getBundle("org.jhotdraw.draw.Labels");
					return labels.getString("attribute.text.text");
				}

				@Override
				public void undo() {
					super.undo();
					editedFigure.willChange();
					editedFigure.setText(oldText);
					editedFigure.changed();
				}

				@Override
				public void redo() {
					super.redo();
					editedFigure.willChange();
					editedFigure.setText(newText);
					editedFigure.changed();
				}
			};
			getDrawing().fireUndoableEditHappened(edit);

			typingTarget.changed();
			typingTarget = null;

			textField.endOverlay();
		}
		// view().checkDamage();
	}

	@Override
	public void keyReleased(KeyEvent evt) {
		if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
			fireToolDone();
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		endEdit();
		fireToolDone();
	}

	public boolean isEditing() {
		return typingTarget != null;
	}

	@Override
	public void updateCursor(DrawingView view, Point p) {
		if (view.isEnabled()) {
			view.setCursor(Cursor
					.getPredefinedCursor(isEditing() ? Cursor.DEFAULT_CURSOR
							: Cursor.CROSSHAIR_CURSOR));
		} else {
			view.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
